package com.yd.cfckc.util;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.StringReader;

/**
 * @author cuiyufeng
 * @email cuiyufeng1458@yundasys.com
 * @date 2020/10/16 2:19 下午
 */
public class XmlFun {
    public static void main(String[] args) throws Exception{
        String xml="<req op=\"getcompanylist\" range=\"10,20\"> <h> <ver>1294738097072</ver> <company>321300</company> <user>1004</user> <pass>202cb962ac59075b964b07152d234b70</pass> <dev1>1001</dev1> <dev2>1001</dev2> </h> </req>";
        long start = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            String[] a=XmlFun.getTags(xml);
        }
        System.out.println((System.currentTimeMillis() - start) / 1000.0);

        String[] a=XmlFun.getTags(xml);
        System.out.println(a[0]);
        System.out.println(a[1]);
        System.out.println(a[2]);
        System.out.println(a[3]);
        System.out.println(a[4]);
        System.out.println(a[5]);
        System.out.println(a[6]);
    }

    /**
     * ��ѯָ��xml�ļ���ǩ��Ӧ������
     * @param sXML
     * @param sTag
     * @param sAttr
     * @return
     * @throws Exception
     */
    public static String getAttribute(String sXML, String sTag, String sAttr)
            throws Exception {
        QName qTag = new QName(sTag);
        QName qAttr = new QName(sAttr);
        String sRet = "";
        StringReader sReader = new StringReader(sXML);
        XMLInputFactory xFactory = XMLInputFactory.newInstance();
        XMLStreamReader xReader;
        for (xReader = xFactory.createXMLStreamReader(sReader); xReader.hasNext();) {
            int event = xReader.next();
            if (event == 1 && xReader.getName().equals(qTag)) {
                sRet = xReader.getAttributeValue(null, qAttr.getLocalPart());
                break;
            }
        }
        xReader.close();
        return sRet;
    }

    /**
     * ��ѯָ��xml�ļ���Ӧ��ǩ������
     * @param sXML
     * @param sTag
     * @return
     * @throws Exception
     */
    public static String getTagFirstValue(String sXML, String sTag) throws Exception {
        QName qTag = new QName(sTag);
        String sRet = "";
        StringReader sReader = new StringReader(sXML);
        XMLInputFactory xFactory = XMLInputFactory.newInstance();
        XMLStreamReader xReader;
        for (xReader = xFactory.createXMLStreamReader(sReader); xReader.hasNext();) {
            int event = xReader.next();
            if (event == 1 && xReader.getName().equals(qTag)) {
                sRet = xReader.getElementText();
                break;
            }
        }
        xReader.close();
        return sRet;
    }

    /**
     * ��ѯָ��xml�ļ���Ӧ��ǩ������
     * @param sXML
     * @param sTag
     * @return
     * @throws Exception
     */
    public static String[] getTagAllValue(String sXML, String sTag) throws Exception {
        QName qTag = new QName(sTag);
        StringBuffer buffer = new StringBuffer();
        StringReader sReader = new StringReader(sXML);
        XMLInputFactory xFactory = XMLInputFactory.newInstance();
        XMLStreamReader xReader;
        for (xReader = xFactory.createXMLStreamReader(sReader); xReader.hasNext();) {
            int event = xReader.next();
            if (event == 1 && xReader.getName().equals(qTag)) {
                buffer.append(",").append(xReader.getElementText());
            }
        }
        xReader.close();
        if(buffer.length()<1){
            return null;
        }
        return buffer.toString().substring(1).split(",");
    }

    public static String[] getTags(String sXML) throws Exception {
        QName qAttr = new QName("op");
        QName qTag0 = new QName("req");
        QName qTag1 = new QName("ver");
        QName qTag2 = new QName("company");
        QName qTag3 = new QName("user");
        QName qTag4 = new QName("pass");
        QName qTag5 = new QName("dev1");
        QName qTag6 = new QName("dev2");
        String[] aRet = new String[7];

        StringReader sReader = new StringReader(sXML);
        XMLInputFactory xFactory = XMLInputFactory.newInstance();
        XMLStreamReader xReader;
        for (xReader = xFactory.createXMLStreamReader(sReader); xReader
                .hasNext();) {
            int event = xReader.next();
            if (event == 1) {
                if (xReader.getName().equals(qTag0)) {
                    aRet[0] = xReader.getAttributeValue(null,
                            qAttr.getLocalPart());
                } else if (xReader.getName().equals(qTag1)) {
                    aRet[1] = xReader.getElementText();
                } else if (xReader.getName().equals(qTag2)) {
                    aRet[2] = xReader.getElementText();
                } else if (xReader.getName().equals(qTag3)) {
                    aRet[3] = xReader.getElementText();
                } else if (xReader.getName().equals(qTag4)) {
                    aRet[4] = xReader.getElementText();
                } else if (xReader.getName().equals(qTag5)) {
                    aRet[5] = xReader.getElementText();
                } else if (xReader.getName().equals(qTag6)) {
                    aRet[6] = xReader.getElementText();
                }
            }
        }
        xReader.close();
        return aRet;
    }

    /**
     * ͨ��xsdУ��xml����
     * @param xmlContent
     * @param xsdFileName
     * @return
     */
    public static int validateXMLByXSD(String xmlContent, String xsdFileName) {
        try {
			/*
			// 1 factory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(true);
			factory.setNamespaceAware(true);
			SAXParser parser = factory.newSAXParser();
			parser.setProperty(
					"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
					"http://www.w3.org/2001/XMLSchema");
			parser.setProperty(
					"http://java.sun.com/xml/jaxp/properties/schemaSource",
					"file:" + xsdFileName);

			// 2 reader
			Document xmlDocument = DocumentHelper.parseText(xmlContent);

			// 3 validator
			SAXValidator validator = new SAXValidator(parser.getXMLReader());
			XMLErrorHandler errorHandler = new XMLErrorHandler();
			validator.setErrorHandler(errorHandler);
			validator.validate(xmlDocument);

			// 4 writer
			//XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
			if (errorHandler.getErrors().hasContent()) {
				//System.out.println("XML�ļ�ͨ��XSD�ļ�У��ʧ�ܣ�");
				//writer.write(errorHandler.getErrors());
				return 1;
			} else {
				//System.out.println("XML�ļ�ͨ��XSD�ļ�У��ɹ���");
				return 0;
			}
			*/
            return 1;

        } catch (Exception ex) {
            //System.out.println("XML�ļ�: " + xml + " ͨ��XSD�ļ�:"
            //+ xsdFileName + "����ʧ�ܡ�\nԭ��\n " + ex.getMessage());
            //ex.printStackTrace();
            return 2;
        }
    }
}
